<?php
namespace App\Service\Front;

use Illuminate\Support\Facades\DB;

class CasexqService extends BaseService
{
    //原创案例-上传

    //选项
    public function getCateList($pid = 0)
    {
        $res = $this->cateOption(2, $pid);
        $list = [];
        if (count($res)) {
            foreach ($res as $v) {
                $v = (array) $v;
                $list[] = [
                    'id' => $v['id'],
                    'name' => $v['title'],
                ];
            }
        }
        if (!$pid) {
            $arys[] = [
                'titlename' => '一级分类',
                'groupname' => $list,
            ];
        } else {
            $arys[] = [
                'titlename' => '二级分类',
                'typename' => $list,
            ];
        }
        return $arys;
    }

    //获取子分类IDS
    public function getCateids($pid)
    {
        $ids = [(int)$pid];
        $res = $this->cateOption(2, $pid);
        if (!count($res)) {
            return $ids;
        }
        foreach ($res as $v) {
            $ids[] = $v->id;
        }
        return $ids;
    }

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     */
    public function getIndex($cateid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCaseXq)->where('del', 0);
        if ($cateid) {
            $cateids = $this->getCateids($cateid);
            $db = $db->whereIn('cate_id', $cateids);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //企业信息
            $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            $cname = empty($comOne) ? '-' : $comOne['cname'];
            $clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'yaoqiu' => $v['yaoqiu'],
                'cname' => $cname,
                'clogo' => $clogo,
                'start_day' => $v['start_day'],
                'end_day' => $v['end_day'],
                'price' => toNumber($v['price']),
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     * @param int $id 案例ID
     * @param int $uid 用户ID
     */
    public function getOne($id, $uid)
    {
        $res = (array) $this->dbConn($this->dbCaseXq)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //企业信息
        $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['cid'])->first();
        $cname = empty($comOne) ? '-' : $comOne['cname'];
        //状态
        $statuss = $this->casexqStatus;
        $status_name = array_key_exists($res['status'], $statuss) ? $statuss[$res['status']] : '-';
        //类型
        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $res['cate_id'])->first();
        $cate_name = empty($cateOne) ? '-' : $cateOne['title'];
        //工作方式
        $work_types = $this->casexq_work_types;
        $work_type_name = array_key_exists($res['work_type'], $work_types) ? $work_types[$res['work_type']] : '';
        //需要技能
        $skills = $res['work_skill'] ? explode('||', $res['work_skill']) : [];
        $work_skill = count($skills) ? implode('，', $skills) : '';
        //学历
        $edus = $this->casexq_edus;
        $edu_name = array_key_exists($res['education'], $edus) ? $edus[$res['education']] : '';
        //参与地区
        $areaOne = (array) $this->dbConn($this->dbArea)->where('id', $res['area_id'])->first();
        $area_name = empty($areaOne) ? '-' : $areaOne['title'];
        //收藏
        $is_collect = 0;
        if ($uid) {
            $collectOne = (array) $this->dbConn($this->dbCasexqCollect)
                ->where('uid', $uid)
                ->where('case_id', $id)
                ->first();
            $is_collect = empty($collectOne) ? 0 : 1;
        }
        //订单状态
        $orderOne = (array) $this->dbConn($this->dbCxOrder)
            ->where('del', 0)
            ->where('case_id', $id)
            ->first();
        $order_status = 0;
        $order_status_name = '未申请';
        $orderno = '';
        if (!empty($orderOne)) {
            $order_status = $orderOne['status'];
            $orderStatuss = $this->casexqOrderStatuss;
            if (array_key_exists($orderOne['status'], $orderStatuss)) {
                $order_status_name = $orderStatuss[$orderOne['status']];
            }
            $orderno = $orderOne['orderno'];
        }
        $arys = [
            'title' => $res['title'],
            'price' => toNumber($res['price']),
            'cate_name' => $cate_name,
            'work_type_name' => $work_type_name,
            'yaoqiu' => $res['yaoqiu'],
            'chanquan' => $res['chanquan'] ? htmlspecialchars_decode($res['chanquan']) : '',
            'work_skill' => $work_skill,
            'work_year' => $res['work_year'] ? $res['work_year'].'年' : '不限',
            'edu_name' => $edu_name,
            'area_name' => $area_name,
            'day_hour' => $res['day_hour'],
            'item_hour' => $res['item_hour'],
            'team_num' => $res['team_num'],
            'end_day' => $res['end_day'],
            'is_collect' => $is_collect,
            'status' => $res['status'],
            'status_name' => $status_name,
            'orderno' => $orderno,
            'order_status' => $order_status,
            'order_status_name' => $order_status_name,
        ];
        return $this->toback(0, '', $arys);
    }

//    //添加
//    public function add($pas)
//    {
//        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $pas['cate_id'])->first();
//        if (empty($cateOne)) {
//            return $this->toback(-2, '分类不存在');
//        }
//        //数据入库
//        $lastid = $this->dbConn($this->dbCase)->insertGetId($pas);
//        if (!$lastid) {
//            return $this->toback(-1, '发布失败');
//        }
//        return $this->toback();
//    }

    /**
     * 相似需求
     * @param int $cate_id
     * @param array $pages【start，limit】
     */
    public function getAlmostList($cate_id = 0, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCaseXq)->where('del', 0);
        if ($cate_id) {
            $db = $db->where('cate_id', $cate_id);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $statuss = $this->casexqStatus;
        $work_types = $this->casexq_work_types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //状态
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '-';
            //类型
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['cate_id'])->first();
            $cate_name = empty($cateOne) ? '-' : $cateOne['title'];
            //工作方式
            $work_type_name = array_key_exists($v['work_type'], $work_types) ? $work_types[$v['work_type']] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'status_name' => $status_name,
                'cate_name' => $cate_name,
                'work_type_name' => $work_type_name,
                'price' => toNumber($v['price']),
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //兼职设计师
    public function getDesignerList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDesigner)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //头像
            $head = $v['head'];
            if (substr($head, 0, 4) != 'http') {
                $head = file_exists($head) ? env('DOMAIN').'/'.$head : '';
            }
            //分类
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['cate_id'])->first();
            $arys[] = [
                'id' => $v['id'],
                'realname' => $v['realname'],
                'head' => $head,
                'work_year' => $v['work_year'],
                'cate_name' => empty($cateOne) ? '-' : $cateOne['title'],
                'score' => $v['score'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 收藏设置
     * @param int $uid
     * @param int $caseid
     */
    public function setCollect($uid, $caseId)
    {
        $res = (array) $this->dbConn($this->dbCasexqCollect)
            ->where('uid', $uid)
            ->where('case_id', $caseId)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已收藏');
        }
        $lastid = $this->dbConn($this->dbCasexqCollect)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '收藏失败');
        }
        return $this->toback();
    }

    /**
     * 转发设置
     * @param int $uid
     * @param int $caseid
     */
    public function setForward($uid, $caseId)
    {
        $res = (array) $this->dbConn($this->dbCasexqForward)
            ->where('uid', $uid)
            ->where('case_id', $caseId)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已转发');
        }
        $lastid = $this->dbConn($this->dbCasexqForward)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '转发失败');
        }
        return $this->toback();
    }

    /**
     * 举报
     * @param int $uid
     * @param int $caseid
     * @param string $cnt
     */
    public function setJubao($uid, $caseId, $cnt)
    {
        $lastid = $this->dbConn($this->dbCasexqJubao)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'cnt' => $cnt,
            'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '反馈失败');
        }
        return $this->toback();
    }

    //发布相似需求
    public function setAlmost($id, $cid)
    {
        $res = (array) $this->dbConn($this->dbCaseXq)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有该需求');
        }
        $newData = $res;
        unset($newData['id']);
        $newData['cid'] = $cid;
        $newData['addtime'] = $newData['uptime'] = toDate();
        $lastid = $this->dbConn($this->dbCaseXq)->insertGetId($newData);
        if (!$lastid) {
            return $this->toback(-1, '发布失败');
        }
        return $this->toback();
    }

    /**
     * 下单申请
     * @param int $uid
     * @param int $caseid
     */
    public function orderApply($uid, $caseid)
    {
        $caseOne = (array) $this->dbConn($this->dbCaseXq)
            ->where('del', 0)
            ->where('id', $caseid)
            ->first();
        if (empty($caseOne)) {
            return $this->toback(-2, '该需求不存在');
        }
        $orderOne = (array) $this->dbConn($this->dbCxOrder)
            ->where('del', 0)
            ->where('case_id', $caseid)
            ->where('uid', $uid)
            ->first();
        if (!empty($orderOne) && $orderOne['status']) {
            return $this->toback(-2, '已申请');
        }
        $orderno = toOrderno();
        $oData = [
            'uid' => $uid, 'case_id' => $caseid, 'price' => $caseOne['price'],
            'orderno' => $orderno, 'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbCxOrder)->insertGetId($oData);
        if (!$lastid) {
            return $this->toback(-1, '申请失败');
        }
        //更新需求状态
        $this->dbConn($this->dbCaseXq)->where('id', $caseid)->update(['status' => 2]);
        return $this->toback();
    }
}